package com.mobiusws.util;


import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CSVUtil extends ExportingUtil {
	FileWriter fw = null;
	
	public  CSVUtil( String strOutFilePath )
	{
		try
		{
			fw = new FileWriter(strOutFilePath);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
    }

	public static String ConvertDate(String strDate) {
		
		String strRe = null;
		
		int ilen = strDate.length();
		if(ilen==8) //day
		{
			SimpleDateFormat inFormat = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
			
			try {
				Date inDate = inFormat.parse(strDate);
				
				strRe = outFormat.format(inDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			
		}
		else if(ilen>8 && ilen<=12)
		{
			SimpleDateFormat inFormat = new SimpleDateFormat("yyyyMMddHH");
			SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			try {
				Date inDate = inFormat.parse(strDate);
				
				strRe = outFormat.format(inDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
			
		return strRe;
	}

	public void AddTitle(ExportReport reportStruct) 
	{
		try
		{
			//write the report name
			//fw.write(reportStruct.getReportName()+"\r\n");
			
			//write the date or date/hour
			//String strDate = ConvertDate(reportStruct.getReportDate());
			//if(strDate==null)  strDate=reportStruct.getReportDate(); // do nothing
			//fw.write(strDate+"\r\n");
			
			//write the title
//			System.out.println("fw="+(fw==null));
//			System.out.println("reportStruct="+(reportStruct==null));
			fw.write(reportStruct.getReportTitle()+"\r\n");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public boolean AddBody(ResultSet rs, ExportReport reportStruct, String strCountryCode, long maskBits, int iLen) {
		
		boolean bSuccess=false;

		//StringBuffer strBuf=new StringBuffer();
		String msisdn =  "";
		try {
			while(rs.next())
			{
				//strBuf.delete(0, strBuf.length());
				
				for (int i = 0; i < iLen; i++) 
				{
					if(reportStruct.getReportDate().length()==10 && ("suspect".compareTo(reportStruct.getReportName())==0 || "simbox".compareTo(reportStruct.getReportName())==0) && "verify".compareToIgnoreCase(reportStruct.getTableColumnsArray()[i])==0) //hourly verify convert
					{
						int iVerify = rs.getInt(i + 1);
						if(iVerify==1)
						{
							fw.write("F,");
						}
						else if(iVerify==2)
						{
							fw.write("V,");
						}
						else
						{
							fw.write("O,");
						}
					}
					else if(reportStruct.getTableColumnsArray()[i].indexOf("msisdn")>=0 && !reportStruct.getTableColumnsArray()[i].startsWith("msisdn_f"))  
					{
						msisdn = rs.getString(i + 1);
						
						if(strCountryCode!=null && msisdn.startsWith(strCountryCode)) //remove the country code prefix if strCountryCode!=null
						{
							msisdn = msisdn.substring(strCountryCode.length());
						}
						
						if(maskBits>0) //unmask the msisdn
						{
							msisdn = intXORMask(msisdn, maskBits);
						}
						
						fw.write(msisdn+",");

					}
					else if("imei".equals(reportStruct.getTableColumnsArray()[i].trim()))
					{
						fw.write(ExportingUtil.getCorrectString(rs.getString(i + 1), reportStruct) + ",");
					}
					else if("imsi".equals(reportStruct.getTableColumnsArray()[i].trim()))
					{
						fw.write(ExportingUtil.getCorrectString(rs.getString(i + 1), reportStruct) + ",");
					}
					else if("cell_id".equals(reportStruct.getTableColumnsArray()[i].trim()))
					{
						fw.write(ExportingUtil.getCorrectString(rs.getString(i + 1), reportStruct) + ",");
					}
					else if(i==iLen-1)
					{
						fw.write(rs.getString(i + 1));
					}
					else
					{
						//strBuf.append(rs.getString(i + 1)+",");
						fw.write(rs.getString(i + 1)+",");
					}
				}
				//strBuf.delete(strBuf.length()-1, strBuf.length());
				//strBuf.append("\r\n");
				fw.write("\r\n");

				//fw.write(strBuf.toString());
			}
			bSuccess=true;
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bSuccess;

	}
	
	public boolean AddBody(List<List<Object>> ls, ExportReport reportStruct, String strCountryCode, long maskBits, int iLen) {
		
		boolean bSuccess=false;

		//StringBuffer strBuf=new StringBuffer();
		String msisdn =  "";
		List<Object> ob = null;
		try {
			for(int l = 0; l < ls.size(); l++)
			{
				ob = ls.get(l);
				//strBuf.delete(0, strBuf.length());
				
				for (int i = 0; i < iLen; i++) 
				{
					if(reportStruct.getTableColumnsArray()[i].indexOf("msisdn")>=0 && !reportStruct.getTableColumnsArray()[i].startsWith("msisdn_f"))  
					{
						msisdn = ob.get(i).toString();
						
						if(strCountryCode!=null && msisdn.startsWith(strCountryCode)) //remove the country code prefix if strCountryCode!=null
						{
							msisdn = msisdn.substring(strCountryCode.length());
						}
						
						if(maskBits>0) //unmask the msisdn
						{
							msisdn = intXORMask(msisdn, maskBits);
						}
						
						fw.write(msisdn+",");

					}
					else if("imei".equals(reportStruct.getTableColumnsArray()[i].trim()))
					{
						fw.write(ExportingUtil.getCorrectString(ob.get(i).toString(), reportStruct) + ",");
					}
					else if("imsi".equals(reportStruct.getTableColumnsArray()[i].trim()))
					{
						fw.write(ExportingUtil.getCorrectString(ob.get(i).toString(), reportStruct) + ",");
					}
					else if("cell_id".equals(reportStruct.getTableColumnsArray()[i].trim()))
					{
						fw.write(ExportingUtil.getCorrectString(ob.get(i).toString(), reportStruct) + ",");
					}
					else if(i==iLen-1)
					{
						fw.write(ob.get(i).toString());
					}
					else
					{
						//strBuf.append(rs.getString(i + 1)+",");
						fw.write(ob.get(i)+",");
					}
				}
				//strBuf.delete(strBuf.length()-1, strBuf.length());
				//strBuf.append("\r\n");
				fw.write("\r\n");

				//fw.write(strBuf.toString());
			}
			bSuccess=true;
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bSuccess;

	}
	
	public void CloseCSV()
	{
		try
		{
			fw.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	

}
